package main

import (
	"context"
	"io"
	"log"
	"net/http"
	"strings"

	"gitee.com/general252/webdav"
)

func main() {
	log.SetFlags(log.Lshortfile)

	handler := webdav.NewHandler2(
		webdav.Handler{
			Prefix:     "/google/webdav",
			LockSystem: webdav.NewMemLS(),
			Logger: func(request *http.Request, err error) {
				log.Printf(">>>> %s %s %s", request.Method, request.RequestURI, request.Header.Get("Destination"))
			},
		},
		webdav.NewDirFileSystem2(
			func(s string) (string, error) {
				// get real file path
				return s, nil
			},
			func(ctx context.Context, name string, r io.Reader) error {
				// upload file
				log.Println(name)
				return nil
			},
		),
	)

	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		if strings.HasPrefix(req.RequestURI, handler.Prefix) {
			handler.ServeHTTP(w, req)
		}
	})

	log.Fatal(http.ListenAndServe(":8888", nil))
}
